package cn.com.winning.ecare.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Locale;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;

public class LocationUtils {
	String key = "F9da85afead8b6e9c4738e5e5b79eb97";
	
	public String getCityName(Context context) { 
        LocationManager locationManager; 
        String contextString = Context.LOCATION_SERVICE; 
        locationManager = (LocationManager) context.getSystemService(contextString); 
        Criteria criteria = new Criteria(); 
        criteria.setAccuracy(Criteria.ACCURACY_FINE); 
        criteria.setAltitudeRequired(false); 
        criteria.setBearingRequired(false); 
        criteria.setCostAllowed(false); 
        criteria.setPowerRequirement(Criteria.POWER_LOW); 
        String cityName = ""; 
        // 取得效果最好的criteria 
        String provider = locationManager.getBestProvider(criteria, true); 
        if (provider == null) { 
            return ""; 
        } 
        // 得到坐标相关的信息 
        Location location = locationManager.getLastKnownLocation(provider); 
        if (location == null) { 
            return ""; 
        } 
 
        if (location != null) { 
            double latitude = location.getLatitude(); 
            double longitude = location.getLongitude(); 
            // 更具地理环境来确定编码 
            Geocoder gc = new Geocoder(context, Locale.getDefault()); 
            try { 
                // 取得地址相关的一些信息\经度、纬度 
                List<Address> addresses = gc.getFromLocation(latitude, longitude, 1); 
                StringBuilder sb = new StringBuilder(); 
                if (addresses.size() > 0) { 
                    Address address = addresses.get(0); 
                    sb.append(address.getLocality()).append("\n"); 
                    cityName = sb.toString(); 
                } 
            } catch (IOException e) { 
            } 
        } 
        return cityName; 
    } 
	

	
//	public String getAddress(String latValue, String longValue){
//		String location = getJsonLocation(latValue, longValue);
//		location = getLocation(makeResults(location));
//		return location;
//	}
//	
//	private String getJsonLocation(String latValue, String longValue){
//		String urlStr = "http://api.map.baidu.com/geocoder?location=" + latValue + "," + longValue + "&output=json&key=" + key;   
//		HttpClient httpClient = new DefaultHttpClient();  
//           String responseData = "";  
//           try{  
//               //向指定的URL发送Http请求  
//               HttpResponse response = httpClient.execute(new HttpGet(urlStr));  
//               //取得服务器返回的响应  
//               HttpEntity entity = response.getEntity();  
//               BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));  
//               String line = "";  
//               while((line = bufferedReader.readLine()) != null){  
//                   responseData = responseData + line;   
//               }  
//           }  
//           catch (Exception e) {  
//               e.printStackTrace();  
//           }  
//             return responseData;
//	}
//	
//	private String makeResults(String result){
//		String dealResult = result.substring(0, result.indexOf("result") +8) + "[" + result.substring(result.indexOf("result") +8, result.length()-1) + "]}";
//		return dealResult;
//	}
//	
//	private String getLocation(String str){
//		  JSONArray jsonObjs;
//		  String location = "";
//		try {
//			 jsonObjs = new JSONObject(str).getJSONArray("result"); 
//	            //取出数组中第一个json对象(本示例数组中实际只包含一个元素) 
//	            JSONObject jsonObj = jsonObjs.getJSONObject(0); 
//	          //解析得formatted_address值 
//	            String city = jsonObj.getString("city");
//		        location = city;
//		} catch (JSONException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} 
//          //取出数组中第一个json对象(本示例数组中实际只包含一个元素) 
//          return location;
//	}
   
}